﻿using Demo.Microservice.Services;
using Demo.Microservice.Services.Dtos;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
using Volo.Abp.Application.Dtos;
using Volo.Abp.AspNetCore.Mvc;

namespace Demo.Microservice.Controllers
{

    /// <summary>
    /// 用户控制器（模拟微服务业务子系统）
    /// </summary>
    [ApiController]
    [Route("/api/user")]
    public class UserController : AbpController, IUserService
    {

        public IUserService _userService { get; set; }
        public UserController()
        {

        }

        /// <summary>
        /// 创建用户
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<UserInfoDto> CreateAsync(UserInfoDto input)
        {
            try
            {

                return await _userService.CreateAsync(input);
            }
            catch (Exception e )
            {

                throw;
            }
        }

        /// <summary>
        /// 删除用户（增加权限校验）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpDelete]
        [Authorize(Policy = "Delete")]//Delete是在AuthService项目中生成的授权策略
        public async Task DeleteAsync(Guid id)
        {
            // 1、获取用户信息
            ClaimsPrincipal claimsPrincipal = HttpContext.User;
            // 2、执行删除操作
            await _userService.DeleteAsync(id);
        }

        /// <summary>
        /// GetAsync
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("single")]
        [Authorize(Policy = "select")]
        public async Task<UserInfoDto> GetAsync([FromQuery] Guid id)
        {
            return await _userService.GetAsync(id);
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("list")]
        [Authorize]
        public async Task<PagedResultDto<UserInfoDto>> GetListAsync([FromQuery] PagedAndSortedResultRequestDto input)
        {
            return await _userService.GetListAsync(input);
        }

        /// <summary>
        /// 查询单个
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet("id")]
        [Authorize("select")]
        public async Task<UserInfoDto> GetSingleUser([FromQuery] string userId)
        {
            return await _userService.GetSingleUser(userId);
        }

        /// <summary>
        /// Update
        /// </summary>
        /// <param name="id"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<UserInfoDto> UpdateAsync(Guid id, UserInfoDto input)
        {
            return await _userService.UpdateAsync(id, input);
        }
    }
}
